<template>
  <div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item>
        <el-input
          v-model="formInline.name"
          placeholder="请输入广告标题"
          size="mini"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="formInline.content"
          placeholder="请输入广告内容"
          size="mini"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="onSubmit"
          size="mini"
          icon="el-icon-search"
          >查找</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="addopen"
          size="mini"
          icon="el-icon-edit"
          >添加</el-button
        >
        <el-button type="primary" size="mini" icon="el-icon-download"
          >导出</el-button
        >
      </el-form-item>
    </el-form>
    <div class="tables">
      <template>
        <el-table border :data="tableData" style="width: 100%">
          <el-table-column prop="id" sortable label="广告ID" align="center">
          </el-table-column>
          <el-table-column prop="name" label="广告标题" align="center">
          </el-table-column>
          <el-table-column prop="content" label="广告内容" align="center">
          </el-table-column>
          <el-table-column prop="url" label="广告图片" align="center">
            <template slot-scope="scope">
              <img :src="scope.row.url" width="50" height="50" alt="" />
            </template>
          </el-table-column>
          <el-table-column prop="position" label="广告位置" align="center">
          </el-table-column>
          <el-table-column prop="link" label="活动链接" align="center">
          </el-table-column>
          <el-table-column prop="enabled" label="是否启用" align="center">
            <template slot-scope="scope">
              <el-tag
                size="mini"
                :type="scope.row.enabled ? 'success' : 'primary'"
                disable-transitions
                >{{ scope.row.enabled ? "启用" : "未启用" }}</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column
            prop="address"
            label="操作"
            align="center"
            width="200"
          >
            <template slot-scope="scope">
              <el-button
                type="primary"
                size="mini"
                @click="handleEdit(scope.$index, scope.row)"
              >
                编辑
              </el-button>
              <el-button
                size="mini"
                type="danger"
                @click="dels(scope.$index, scope.row)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <div class="page">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="get.page"
          :page-sizes="[10, 20, 30, 40, 50, 100]"
          :page-size="get.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <!-- ========================= -->
    <el-dialog title="创建" :visible.sync="dialogVisible" width="50%">
      <el-form
        ref="form"
        :rules="rules"
        :model="form"
        label-width="120px"
        label-position="left"
        style="margin-left: 50px"
      >
        <el-form-item label="广告标题" prop="name">
          <el-input
            v-model="form.name"
            style="width: 300px"
            size="mini"
          ></el-input>
        </el-form-item>
        <el-form-item label="广告内容" prop="content">
          <el-input
            v-model="form.content"
            style="width: 300px"
            size="mini"
          ></el-input>
        </el-form-item>
        <el-form-item label="专题图片" class="inline-form-item" prop="url">
          <el-upload
            class="avatar-uploader"
            action="/api/admin/storage/create"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img v-if="form.url" :src="form.url" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <span>只能上传jpg/png文件,且不超过1024kb</span>
        </el-form-item>
        <el-form-item label="广告位置">
          <el-select
            v-model="form.position"
            style="width: 300px"
            placeholder="请选择"
          >
            <el-option
              v-for="item in options1"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="活动链接">
          <el-input
            v-model="form.link"
            style="width: 300px"
            size="mini"
          ></el-input>
        </el-form-item>
        <el-form-item label="是否启用">
          <el-select
            v-model="form.enabled"
            style="width: 300px"
            placeholder="请选择"
          >
            <el-option
              v-for="item in options2"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button v-if="flag" type="primary" @click="update">确 定</el-button>
        <el-button v-if="!flag" type="primary" @click="addlist"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { adsList, adsredact, adsdel, adsadd, search } from "@/api/promotion";
import moment from "moment";
export default {
  data() {
    return {
      flag: true,
      formLabelWidth: "120px",
      dialogVisible: false,
      form: {
        name: "",
        content: "",
        link: "",
        url: "",
        posion: "1",
        enabled: "true",
      },
      options1: [
        {
          value: 1,
          label: "首页",
        },
      ],
      options2: [
        {
          value: true,
          label: "启用",
        },
        {
          value: false,
          label: "不启用",
        },
      ],
      tableData: [],
      formInline: {},
      total: 0,
      value1: 0,
      value2: 0,
      get: {
        page: 1,
        limit: 20,
        name: "",
        content: "",
        sort: "add_time",
        order: "desc",
      },
      rules: {
        name: [
          { required: true, message: "广告标题不能为空", trigger: "blur" },
        ],
        content: [
          { required: true, message: "广告内容不能为空", trigger: "blur" },
        ],
        url: [{ required: true, message: "广告链接不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getdata();
  },
  methods: {
    getdata() {
      adsList(this.get).then((res) => {
        console.log(res);
        if ((res.data.errmsg = "成功")) {
          this.tableData = res.data.list;
          this.total = res.data.total;
          console.log(this.tableData);
        }
      });
    },
    // 查询
    onSubmit() {
      this.get.page = 1;
      this.get.name = this.formInline.name;
      this.get.content = this.formInline.content;
      this.getdata();
    },

    // 编辑
    handleEdit(a, b) {
      this.flag = true;
      this.dialogVisible = true;
      this.form = b;
    },
    //编辑确定
    update() {
      adsredact(this.form).then((res) => {
        console.log(res);
        this.getdata();
        this.form = {};
        this.dialogVisible = false;
      });
    },
    close() {
      this.dialogVisible = false;
      this.form = {};
    },
    // 删除
    dels(a, b) {
      adsdel(b).then((res) => {
        if (res.errmsg == "成功") {
          this.getdata();
          this.$notify({
            title: "成功",
            message: "删除成功",
            type: "success",
          });
        }
      });
    },
    // 添加打开
    addopen() {
      this.flag = false;
      this.dialogVisible = true;
    },
    //添加
    addlist() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          adsadd(this.form).then((res) => {
            console.log(res);
            this.dialogVisible = false;
            this.getdata();
          });
        } else {
        }
      });
    },
    //分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.get.limit = val;
      this.getdata();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.get.page = val;
      this.getdata();
    },
    formatDate(row, column, cellValue) {
      return moment(cellValue).format("YYYY-MM-DD HH:mm:ss");
    },
    handleAvatarSuccess(res, file) {
      this.form.url = URL.createObjectURL(file.raw);
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-table td,
::v-deep .el-table th {
  padding: 4px 0px;
}
.el-table {
  font-size: 12px;
}
.page {
  margin-top: 40px;
  display: flex;
  justify-content: left;
}
::v-deep.el-form-item label {
  width: 200px;
}
.inline-form-item {
  display: flex;
  align-items: center;
}

::v-deep .inline-form-item .el-form-item__label {
  width: 100px;
  text-align: left;
  padding-right: 10px;
}

::v-deep .inline-form-item .el-form-item__content {
  flex: 1;
  margin-left: 0 !important;
}

::v-deep .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 130px;
  height: 130px;
  line-height: 130px;
  text-align: center;
}
.avatar {
  width: 130px;
  height: 130px;
  display: block;
}
</style>
